﻿// #define NAVTIE_VIBRATION
// #define NAVTIE_IAP

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if NAVTIE_VIBRATION
using WssNative.Vibrations;
#endif

#if NAVTIE_IAP
using WssNative.IAP;
#endif

[DefaultExecutionOrder(1)]
/// <summary>
/// UISettingButton  V0.1.0 版本
/// </summary>
public class UISettingButton : MonoBehaviour
{
    public Button mToggleMusic;
    public Button mToggleSound;
    public Button mToggleVibration;

    public Button mButtonRestore;


    public bool musicState
    {
        get { return AudioManager.Instance.canPlayMusic; }
        set
        {
            AudioManager.Instance.SetMusicOpen(value, true);
            if (mToggleMusic) mToggleMusic.GetComponentInChildren<UIFrameIndex>().index = !value ? 0 : 1;
        }
    }

    public bool soundState
    {
        get { return AudioManager.Instance.canPlaySound; }
        set
        {
            AudioManager.Instance.SetSoundOpen(value);
            if (mToggleSound) mToggleSound.GetComponentInChildren<UIFrameIndex>().index = !value ? 0 : 1;
        }
    }

#if NAVTIE_VIBRATION

    public bool vibrateState
    {
        get { return VibrationController.Instance.canVibrate; }
        set
        {
            VibrationController.Instance.SetVibrate(value);
            if (mToggleVibration) mToggleVibration.GetComponentInChildren<UIFrameIndex>().index = !value ? 0 : 1;
        }
    }

#endif

    private void Start()
    {
#if NAVTIE_IAP
        if(mButtonRestore){
            mButtonRestore.gameObject.SetActive(SdkConfig.Instance.enabledNoAdsButton);
        }
#endif
        musicState = AudioManager.Instance.canPlayMusic;
        if (mToggleMusic) mToggleMusic.onClick.AddListener(OnToggleMusic);

        soundState = AudioManager.Instance.canPlaySound;
        if (mToggleSound) mToggleSound.onClick.AddListener(OnToggleSound);

#if NAVTIE_VIBRATION
        vibrateState = VibrationController.Instance.canVibrate;
        if (mToggleVibration) mToggleVibration.onClick.AddListener(OnToggleVibration);
#endif

    }

    public void OnToggleMusic()
    {
        AudioManager.Instance.Play("btn_click");
        musicState = !musicState;
    }

    public void OnToggleSound()
    {
        AudioManager.Instance.Play("btn_click");
        soundState = !soundState;

        if (soundState == true)
        {
            AudioManager.Instance.Play("btn_click");
        }


    }

#if NAVTIE_VIBRATION
    public void OnToggleVibration()
    {
        AudioManager.Instance.Play("btn_click");
        vibrateState = !vibrateState;

        if (vibrateState)
        {
            VibrationController.Instance.Impact();
        }

    }

#endif

    public void OnRestore()
    {
#if NAVTIE_IAP
        PurchaseManager.Instance.Restore();
#endif
    }

    private void OnDestroy()
    {
        PlayerPrefs.Save();
    }


}
